import 'package:flutter/material.dart';
import 'package:flutter_application_1/provider/user_provider.dart';
import 'package:flutter_application_1/provider/cart_provider.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class UserPage extends StatefulWidget {
  @override
  _UserPageState createState() => _UserPageState();
}

class _UserPageState extends State<UserPage> {
  RefreshController _refreshController = RefreshController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final _userProvider = Provider.of<UserProvider>(context);
    final _cartProvider = Provider.of<CartProvider>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('用户信息'),
      ),
      body: SmartRefresher(
        controller: _refreshController,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  child: Text('登录'),
                  onPressed: () {
                    _refreshController.requestRefresh();
                    _userProvider.login({}).then((_) {
                      _refreshController.refreshCompleted();
                    });
                  },
                ),
                Text('用户名：${_userProvider.user?.username}'),
                Text('手机号：${_userProvider.user?.phone}'),
                Text('购物车：${_cartProvider.cartList.length}'),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
